# -*- encoding: utf-8 -*-
"""
@File    : test.py
@Time    : 2021/8/31 10:15
@Author  : zhouys4
"""
# l1 = [1, 2, 3]
# l2 = [2, 3, 4]
# # print (list(set(l1).union(set(l2))))
# print(list(set(l1).intersection(set(l2))))

from operator import itemgetter
rows = [
    {'name': 'Brian', 'id': 1003},
    {'name': 'David', 'id': 1002},
    {'name': 'John', 'id': 1001},
    {'name': 'Big', 'id': 1004},
    {'name': 'Ag', 'id': 1004}
]


# 改变当前列表
# rows.sort(key = lambda r:r.get("uid"))
# print(rows)

# 新建列表
# rows_by_id = sorted(rows,key = lambda r:r.get("id"))
# print(rows_by_id)

# 使用lambda 根据id和name进行排序
# rows_by_id_name = sorted(rows, key=lambda r: (r['id'],r['name']))
# print(rows_by_id_name)


# 使用operator中的itemgetter,根据id排序
# rows_by_id = sorted(rows, key=itemgetter('id'))
# print(rows_by_id )


rows_by_id_name = sorted(rows, key=itemgetter('id','name'))
print(rows_by_id_name)

#[{'name': 'John', 'id': 1001}, {'name': 'David', 'id': 1002}, {'name': 'Brian', 'id': 1003}, {'name': 'Big', 'id': 1004}, {'name': 'Ag', 'id': 1004}]
#[{'name': 'John', 'id': 1001}, {'name': 'David', 'id': 1002}, {'name': 'Brian', 'id': 1003}, {'name': 'Ag', 'id': 1004}, {'name': 'Big', 'id': 1004}]

